package com.flashcash.solu.view.fragment.presenter;

import com.flashcash.solu.R;
import com.flashcash.solu.app.base.presenter.BaseFragmentPresenterImpl;
import com.flashcash.solu.common.EventCollection;
import com.flashcash.solu.common.TokenManager;
import com.flashcash.solu.view.fragment.MeFraView;
import com.flashcash.solu.widget.xleotoast.XLeoToast;
import com.hwangjr.rxbus.RxBus;
import com.flashcash.kotlin.data.ActivityInfoBean;
import com.flashcash.kotlin.data.HelpCenterUrlAndHotlineBean;
import com.flashcash.kotlin.data.MeInfoBean;

import java.util.ArrayList;

import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @作者:My
 * @创建日期: 2017/6/21 16:59
 * @描述:${TODO}
 * @更新者:${Author}$
 * @更新时间:${Date}$
 * @更新描述:${TODO}
 */

public class MeFraPreImp extends BaseFragmentPresenterImpl implements MeFraPresenter {
    @Override
    public void initUserAccountInfo() {
        final MeFraView view = (MeFraView) mView;
        if (TokenManager.getInstance().hasLogin()) {
            showLoading(null);
            mUserApi.getMeInfo(TokenManager.getInstance().getToken())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new Subscriber<MeInfoBean>() {
                        @Override
                        public void onCompleted() {
                        }

                        @Override
                        public void onError(Throwable e) {
                            dismissLoading();
                            XLeoToast.showMessage(R.string.show_netwok_wrong);
                            if (isAttached()) {
                                view.initUserInfo(new MeInfoBean());
                            }
                        }

                        @Override
                        public void onNext(MeInfoBean meInfoBean) {
                            dismissLoading();
                            if (isAttached() && meInfoBean != null && meInfoBean.getChatAccount()
                                    != null) {
                                TokenManager.storeYWAccount(meInfoBean.getChatAccount().getUserid
                                        (), meInfoBean.getChatAccount().getPassword());
                                view.initUserInfo(meInfoBean);
                            }
                        }
                    });
        } else {
            showLoading(null);
            mUserApi.getActivityList()
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new Subscriber<ArrayList<ActivityInfoBean>>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            if (isAttached()) {
                                XLeoToast.showMessage(R.string.show_netwok_wrong);
                                dismissLoading();
                            }
                        }

                        @Override
                        public void onNext(ArrayList<ActivityInfoBean> activityInfoBeen) {
                            if (isAttached()) {
                                dismissLoading();
                                MeInfoBean bean = new MeInfoBean();
                                bean.setBanner(activityInfoBeen);
                                view.initUserInfo(bean);
                            }
                        }
                    });
        }
    }

    @Override
    public void logout() {
        showLoading(null);
        mUserApi.logout(TokenManager.getInstance().getToken())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        // in any case we should logout even if network is disconnected or token
                        // expired
                        // for example, if token expires, how could user logout successfully?
                        logoutImpl();
                    }

                    @Override
                    public void onNext(ResponseBody response) {
                        logoutImpl();
                    }
                });
    }

    @Override
    public void getHelpCenterUrlAndHotline() {
        final MeFraView view = (MeFraView) mView;
        mUserApi.getHelpCenterUrlAndHotline()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HelpCenterUrlAndHotlineBean>() {
                    @Override
                    public void onCompleted() {}

                    @Override
                    public void onError(Throwable e) {
                        XLeoToast.showMessage(R.string.show_netwok_wrong);
                    }

                    @Override
                    public void onNext(HelpCenterUrlAndHotlineBean response) {
                        view.setHelpCenterUrlAndHotline(response);
                    }
                });
    }

    @Override
    public void login() {
        mView.toLogin();
    }

//    @Override
//    public void requestDisplay() {
//        showLoading(null);
//        mUserApi.display()
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .compose(mView.bindToLifecycle())
//                .subscribe(new Subscriber<DisplayBean>() {
//                    @Override
//                    public void onCompleted() {
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        dismissLoading();
//                        Object object = ACache.get(mView.getBaseActivity()).getAsObject
//                                (FieldParams.ACacheKeys.ACACHE_DISPLAY_BEAN);
//                        if (object != null && object instanceof DisplayBean) {
//                            DisplayBean displayBean = (DisplayBean) object;
//                            if (TextUtils.isEmpty(displayBean.getCustomerMobile())) {
//                                ((MeFraView) mView).responseDisplayEmpty();
//                            } else {
//                                ((MeFraView) mView).responseDisplay(displayBean);
//                            }
//                        } else {
//                            ((MeFraView) mView).responseDisplayEmpty();
//                        }
//                    }
//
//                    @Override
//                    public void onNext(DisplayBean displayBean) {
//                        dismissLoading();
//                        ACache.get(mView.getBaseActivity()).put(FieldParams.ACacheKeys
//                                .ACACHE_DISPLAY_BEAN, displayBean);
//                        if (displayBean == null || TextUtils.isEmpty(displayBean.getCustomerMobile())) {
//                            ((MeFraView) mView).responseDisplayEmpty();
//                        } else {
//                            ((MeFraView) mView).responseDisplay(displayBean);
//                        }
//                    }
//                });
//    }

    private void logoutImpl() {
        TokenManager.getInstance().clear();
//                        TokenManager.removeMessageFromFile(FieldParams.USERNAME);
        //unbind account for ali push
//        PushServiceFactory.getCloudPushService().unbindAccount(new CommonCallback() {
//            @Override
//            public void onSuccess(String s) {
//                XLog.INSTANCE.d("unbind success", 10);
//            }
//
//            @Override
//            public void onFailed(String s, String s1) {
//                XLog.INSTANCE.d("unbind failed", 10);
//            }
//        });
        dismissLoading();
        RxBus.get().post(new EventCollection.LogoutEvent());
        XLeoToast.showMessage(R.string.show_log_out_success);
    }
}
